$(document).ready(function(){
	initPosition();
});


//获取角色列表

function showPosition(positionList){
	for (var i=0;i<positionList.length;i++) {
		$("#positionList").append('<tr  id="position_'+positionList[i].id+'"><td class="positionName">'+positionList[i].name+'</td><td>'+positionList[i].createtime.split(".")[0]+'</td><td><button class="update_btn" onclick="updatePosition(this)">修改</button> <button class="delete_btn" onclick="delPosition(this)">删除</button> </td><td></td></tr>');
	}
}

function initPosition(){
	var url = "/HyperIT/position/findPosition";
	$.get(url,function(data){
		if(data.serviceResult){
			var positionList = data.resultParm.positionlist;
			showPosition(positionList);
		}
		else{
			alert(data.resultInfo);
		}
	});
}

//增加职位

function addPosition(){
	var positionName = prompt("请输入新的职位：","");
	if(positionName !=" " && positionName!=null && positionName!="")
 			{
 				var url = "/HyperIT/position/savePosition";
 				var parm = "&name="+positionName;
 				$.post(url,parm,function(data){
 					if(data.serviceResult){
 						$("#positionList").append('<tr  id="position_'+data.resultParm.position.id+'"><td class="positionName">'+positionName+'</td><td>'+data.resultParm.position.createtime.split(".")[0]+'</td><td><button class="update_btn" onclick="updatePosition(this)">修改</button> <button class="delete_btn" onclick="delPosition(this)">删除</button> </td><td></td></tr>');
 						alert(data.resultInfo);
 					}
 					else{
 						alert(data.resultInfo);
 					}
 				});
 			}
 	 else{
 	 	return falae;
 	 }
}

//删除职位

function delPosition(which) {
		var $thisTr;
		$thisTr = $(which).parent().parent();
		var id = $thisTr.attr("id").split("_")[1];
    	var msg = "确定要删除吗？";
			if (confirm(msg)==true){
				var url = "/HyperIT/position/deletePosition";
				var parm = "&id="+id;
				$.post(url,parm,function(data){
					if(data.serviceResult){
						$thisTr.remove();
						alert(data.resultInfo);
					}
				});
        	}
			else{
				return false;
			}
}

//修改职位

var $thisPosition;
function updatePosition(which) {
	$(".mask").show();
	$thisPosition = $(which).parent().parent();
	$(".btn1").click(function(){
				var newPosition = $("#name").val();
				var url = "/HyperIT/position/updatePosition";
				var parm = "id="+$thisPosition.attr("id").split("_")[1]+"&name="+newPosition;
				$.post(url,parm,function(data){
					if(data.serviceResult){
						$thisPosition.children(".positionName").text(newPosition);
						alert(data.resultInfo);
						maskHidden();
					}
				});
			});
		$("#name").val("");
}


//隐藏弹窗

function maskHidden(){
	$(".mask").hide();
}
